﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Collections;
using System.Data;

namespace DAO
{
    public class GioHangDAO
    {
        private DataProvider provider;

        public GioHangDAO()
        {
            provider = new DataProvider();
        }
        
        public int ThemGioHang(GioHangDTO gioHang)
        {

            string sqlStatement = "INSERT INTO GioHang(NgayDatHang, MaTaiKhoan, TongTien) VALUES ('" 
                + gioHang.NgayDatHang + "', " + gioHang.MaTaiKhoan + ", " + gioHang.TongTien + ")";
            provider.ExecuteNonQuery(sqlStatement);
            ArrayList arrGioHang = new ArrayList();
            try
            {
                string query = "SELECT * FROM GioHang ORDER BY MaGioHang";
                DataTable dt = new DataTable();
                dt = provider.ExecuteQuery(query);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    GioHangDTO item = new GioHangDTO();

                    item.MaGioHang = int.Parse(dt.Rows[i].ItemArray[0].ToString());
                    item.NgayDatHang = dt.Rows[i].ItemArray[1].ToString();
                    item.MaTaiKhoan = int.Parse(dt.Rows[i].ItemArray[2].ToString());
                    item.TongTien = float.Parse(dt.Rows[i].ItemArray[3].ToString());

                    arrGioHang.Add(item);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error: " + ex.Message);
            }
            return ((DTO.GioHangDTO)arrGioHang[arrGioHang.Count-1]).MaGioHang;
        }

        public int CapNhatGioHang(GioHangDTO gioHang)
        {
            string sqlStatement = "UPDATE GioHang SET NgayDatHang='" + gioHang.NgayDatHang + "', MaTaiKhoan="
                + gioHang.MaTaiKhoan + ", TongTien=" + gioHang.TongTien + " WHERE MaGioHang=" + gioHang.MaGioHang;
            
            return provider.ExecuteNonQuery(sqlStatement);
        }

        public int XoaGioHang(GioHangDTO gioHang)
        {
            string sqlStatement = "DELETE FROM GioHang WHERE MaGioHang=" + gioHang.MaGioHang;

            return provider.ExecuteNonQuery(sqlStatement);
        }

        public ArrayList LayDanhSachGioHang()
        {
            ArrayList arrGioHang = new ArrayList();
            try
            {
                string query = "SELECT * FROM GioHang";
                DataTable dt = new DataTable();
                dt = provider.ExecuteQuery(query);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    GioHangDTO item = new GioHangDTO();

                    item.MaGioHang = int.Parse(dt.Rows[i].ItemArray[0].ToString());
                    item.NgayDatHang = dt.Rows[i].ItemArray[1].ToString();
                    item.MaTaiKhoan = int.Parse(dt.Rows[i].ItemArray[2].ToString());
                    item.TongTien = float.Parse(dt.Rows[i].ItemArray[3].ToString());

                    arrGioHang.Add(item);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error: " + ex.Message);
            }
            return arrGioHang;
        }
    }
}
